import Vue from 'vue'
import Vuex from 'vuex'
import areaData from '@/utils/area.js'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isLogin: false,
    userInfo: {}
  },
  mutations: {
    SAVE_USER_INFO (state, newUserInfo) {
      state.userInfo = newUserInfo
    },
    SET_IS_LOGIN (state, newIsLogin) {
      state.isLogin = newIsLogin
    },
    EDIT_USER_INFO (state, { propName, propValue }) {
      state.userInfo[propName] = propValue
    }
  },
  getters: {
    USER_AVATAR (state) {
      const _reg = /^[^http]/
      if (_reg.test(state.userInfo.avatar)) {
        return process.env.VUE_APP_URL + state.userInfo.avatar
      } else {
        return state.userInfo.avatar
      }
    },
    SET_GENDER (state) {
      const genderArr = ['未知', '男', '女']
      return genderArr[state.userInfo.gender]
    },
    SET_AREA (state) {
      return areaData.city_list[state.userInfo.area]
    }
  },
  actions: {},
  modules: {}
})

